package com.demo.leetCode;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class LeetCode003 {
    @Test
    public void lengthOfLongestSubstring() {
        String s = "abcadas";
        Map<Character, Integer> cache = new HashMap<>();
        int pre = 0;
        int i = 1;
        int max = 1;
        String maxStr = s.charAt(0) + "";
        cache.put(s.charAt(0), 0);

        for(; i < s.length(); i++) {
            // 说明有重复的了
            if (cache.containsKey(s.charAt(i)) && cache.get(s.charAt(i)) >= pre) {
                if (max < i - pre) {
                    max = i - pre;
                    maxStr = s.substring(pre, i);
                }
                pre = cache.get(s.charAt(i)) + 1;
            }
            cache.put(s.charAt(i), i);
        }

        if (max < i - pre) {
            max = i - pre;
            maxStr = s.substring(pre, i);
        }

        System.out.println("数量： " + max + "，字符串：" + maxStr);
    }
}
